﻿using CallCenter.Data.Base;
using CallCenter.Data.Models;
using CallCenter.Data.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace CallCenter.Data.BizUtilities
{
    public class AccountUtility : BaseUtility
    {
        public AccountUtility(DataManager manager)
            : base(manager)
        { }

        public string RegisterUser(RegisterViewModel registerUser)
        {
            registerUser.Password = CryptUtils.GetPasswordEncrypted(registerUser.Password);
            string Result = "";
            using (var command = this.Factory.GetCommand("usp_UserCrud", true, false))
            {

                command.AddParameter("@pMode", 0)
                    .AddParameter("@pUserName", registerUser.UserName.ToLower())
                    .AddParameter("@pPassword", registerUser.Password)
                    .AddParameter("@pFullName", registerUser.FullName)
                    .AddParameter("@RoleID", registerUser.RoleId)
                    .AddParameter("@pEmail", registerUser.Email)
                    .AddParameter("@ContactId", registerUser.ContactId);


                var param = this.Factory.AddParameter(command, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                var rowsaffected = command.ExecuteNonQuery();

                if (rowsaffected > 0)
                {
                    var filePath = HostingEnvironment.MapPath("~/Content/Emails/NewUserRegistration.html");
                    var fileContent = File.ReadAllText(filePath);

                    EmailUtility.SendMail("New Account Registration",
                                    string.Format(fileContent, registerUser.FullName, registerUser.UserName),
                                    registerUser.Email);
                    //this.Bridge.RoleUser.InsertRoleUser(new RoleUserModel { RoleId = registerUser.RoleId, UserId = registerUser.Id });
                    Result = "User is Created Successfully !!!!";
                }
            }

            return Result;
        }


        public bool UpdateUser(RegisterViewModel registerUser)
        {
            registerUser.Password = CryptUtils.GetPasswordEncrypted(registerUser.Password);

            using (var command = this.Factory.GetCommand("usp_UserCrud", true, false))
            {
                command.AddParameter("@pMode", 2)
                    .AddParameter("@pUserName", registerUser.UserName)
                    .AddParameter("@pPassword", registerUser.Password)
                    .AddParameter("@pFullName", registerUser.FullName)
                    .AddParameter("@pEmail", registerUser.Email)
                    .AddParameter("@pConfirmPassword", registerUser.ConfirmPassword)
                    .AddParameter("", "", isInputParameter: false);

                command.ExecuteNonQuery();

                this.Bridge.RoleUser.InsertRoleUser(new RoleUserModel { RoleId = registerUser.RoleId, UserId = registerUser.Id });

                return true;
            }

            return false;
        }


        public string getContactFirstname(int id)
        {

            string result = "";
            string sql = "select firstName from cccontacts where id = " + id;

            try
            {
                result = this.Factory.ExecuteScalar(sql).ToString();
            }
            catch { }
            return result;


        }
        public string getuserIdByContactId(int id)
        {

            string result = "";
            string sql = "select userId from cccontacts where id = " + id;

            try
            {
                result = this.Factory.ExecuteScalar(sql).ToString();
            }
            catch { }
            return result;


        }

        public void UpdateUserEmployee(int UserId,int id)
        {
            string sql = "update tbl_Users set employeeid = " + id + " where Id = '" + UserId + "'";

            this.Factory.ExecuteNonQuery(sql);

        }
        //Gets Roles
        public RegisterViewModel GetUser(string username)
        {
            using (var cmd = this.Factory.GetCommand("usp_UserCrud", true, false)
                .AddParameter("@pMode", 1)
                .AddParameter("@pUserName", username))
            {
                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                return this.Factory.ExecuteObject<RegisterViewModel>(cmd);
            }
        }

        public int GetUserIDByUserNames(string username)
        {
            int result = 0;
            string sql = "select top(1) Id from tbl_Users where LOWER(UserName) = '" + username + "'";

            try
            {
                result = Convert.ToInt32(this.Factory.ExecuteScalar(sql));
            }
            catch { }
            return result;

        }


        public List<RegisterViewModel> GetListOfUsers()
        {
            string sql = "select * from tbl_users where (active <> 0 or active is null) order by UserName";

            return this.Factory.ExecuteObjects<RegisterViewModel>(sql);

        }


        public List<RegisterViewModel> GetListOfActiveUsers()
        {
            string sql = "select * from tbl_users where (active <> 0 or active is null) order by UserName";

            return this.Factory.ExecuteObjects<RegisterViewModel>(sql);

        }

        public void Deleteuser(int nContactID)
        {
            
                string sql = "update tbl_users set active = 'false'  Where id = " + nContactID;
                 this.Factory.ExecuteNonQuery(sql);

        }


        public bool ValidateUser(string username, string password)
        {
            //ToDo : Check user in database and then return either true or false
            password = CryptUtils.GetPasswordEncrypted(password);

            var regUser = this.GetUser(username);

            if (regUser.UserName == username && regUser.Password == password)
                return true;

            return false;
        }





    }
}
